package com.example.maybeaccount.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.maybeaccount.InputDialog;
import com.example.maybeaccount.Mode.Check_ok;
import com.example.maybeaccount.Mode.SqliteDB;
import com.example.maybeaccount.R;
import com.example.maybeaccount.Spinner.CommonSpinner;
import com.example.maybeaccount.datepicker.CustomDatePicker;
import com.example.maybeaccount.datepicker.DateFormatUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Fragment_outcome extends Fragment {

    private String lv1_option;
    private String lv2_option;
    private String account_option;
    private String member_option;
    private String amount;

    private TextView input_time;

    private CustomDatePicker mTimerPicker;

    private EditText input_business;
    private EditText input_project;
    private EditText input_remark;

    private CommonSpinner<String> spinner_factor;
    private CommonSpinner<String> spinner_factor2;
    private CommonSpinner<String> spinner_factor3;

    private Context context;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        final View v=inflater.inflate(R.layout.fragment_outcome, container,false);


        final  Button choose_amount =v.findViewById(R.id.choose_amount);
        choose_amount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputDialog dialog1 = new InputDialog(getActivity(), R.style.Theme_AppCompat_Dialog, new InputDialog.PriorityListener() {
                    @Override
                    public void setActivityText(String string) {
                        if(Check_ok.is_double(string)){
                            choose_amount.setText(string);
                            amount = string;
                        }else{
                            Toast.makeText(getContext(),"请输入小于8位的数值",Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void setAccountData(String name, String category, String amount, String remark) {

                    }
                });
                dialog1.show();
            }
        });

        final ImageButton switch_eye = v.findViewById(R.id.switch_eye);
        switch_eye.setOnClickListener(new View.OnClickListener() {
            int eye_flag=1;
            @Override
            public void onClick(View v) {
                if(eye_flag==0){
                    switch_eye.setImageResource(R.drawable.eye);
                    eye_flag=1;
                    choose_amount.setText(amount);
                }
                else if(eye_flag==1){
                    switch_eye.setImageResource(R.drawable.closeeye);
                    eye_flag=0;
                    amount = choose_amount.getText().toString();
                    choose_amount.setText("**·*");

                }


            }
        });

        List<String> data_list;
        final List<String> data_list2;
        final List<String> data_list3;
        final List<String> data_list4;
        final List<String> data_list5;
        final List<String> data_list6;
        final List<String> data_list7;


        data_list= Arrays.asList(getResources().getStringArray(R.array.menu1));
        data_list2 = Arrays.asList(getResources().getStringArray(R.array.menu2_self));
        data_list3 = Arrays.asList(getResources().getStringArray(R.array.menu2_play));
        data_list4 = Arrays.asList(getResources().getStringArray(R.array.menu2_shopping));
        data_list5 = Arrays.asList(getResources().getStringArray(R.array.menu2_food));
        data_list6 = Arrays.asList(getResources().getStringArray(R.array.menu2_transport));
        data_list7 = Arrays.asList(getResources().getStringArray(R.array.menu2_else));
        LinearLayout ll1 = (LinearLayout)v.findViewById(R.id.geology_age);
        LinearLayout ll2 = (LinearLayout)v.findViewById(R.id.geology_age2);
        spinner_factor=new CommonSpinner<>(getContext(),data_list.get(0),data_list);
        spinner_factor2=new CommonSpinner<>(getContext(),data_list4.get(0),data_list4);
        ll1.addView(spinner_factor);
        ll2.addView(spinner_factor2);
        spinner_factor.setSpinner_ClickListener(new CommonSpinner.Spinner_ClickListener() {
            @Override
            public void ClickListener(String data) {
                if(data.equals(getResources().getString(R.string.DIY))){
                    spinner_factor.getEditView().setText("");
                    EditText et=spinner_factor.getEditView();
                    et.setFocusable(true);
                    et.setFocusableInTouchMode(true);
                    InputMethodManager inputManager =(InputMethodManager)et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if(inputManager!=null) {
                        et.requestFocus();
                        inputManager.showSoftInput(et, 0);
                    }
                    spinner_factor2.changedatas(data_list2);
                }
                else if(data.equals("娱乐")){
                    spinner_factor2.changedatas(data_list3);
                }else if(data.equals("购物")){
                    spinner_factor2.changedatas(data_list4);
                }else if(data.equals("餐饮")){
                    spinner_factor2.changedatas(data_list5);
                }else if(data.equals("交通")){
                    spinner_factor2.changedatas(data_list6);
                }else {
                    spinner_factor2.changedatas(data_list7);
                }
            }
        });

        spinner_factor2.setSpinner_ClickListener(new CommonSpinner.Spinner_ClickListener() {
            @Override
            public void ClickListener(String data) {
                if(data.equals(getResources().getString(R.string.DIY))){
                    spinner_factor2.getEditView().setText("");
                    EditText et=spinner_factor2.getEditView();
                    et.setFocusable(true);
                    et.setFocusableInTouchMode(true);
                    InputMethodManager inputManager =(InputMethodManager)et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if(inputManager!=null) {
                        et.requestFocus();
                        inputManager.showSoftInput(et, 0);
                    }
                }
            }
        });





        Spinner account = v.findViewById(R.id.spinner_account);
        ArrayList<String> accountNameList = SqliteDB.getInstance(context).getAllAccountName();
        final String[] accountList = accountNameList.toArray(new String[accountNameList.size()]);
        // 建立Adapter并且绑定数据源
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, accountList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        account.setAdapter(adapter);
        account.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override

            public void onItemSelected(AdapterView<?> parent, View view,

                                       int pos, long id) {
                //String[] account_options = getResources().getStringArray(R.array.account);

                account_option=accountList[pos];

            }
            @Override

            public void onNothingSelected(AdapterView<?> parent) {

                // Another interface callback

            }

        });

        final List<String> data_list8;
        data_list8= Arrays.asList(getResources().getStringArray(R.array.member));


        LinearLayout ll3 = v.findViewById(R.id.spinner_member);
        spinner_factor3=new CommonSpinner<>(getContext(),data_list8.get(0),data_list8);
        ll3.addView(spinner_factor3);
        spinner_factor3.setSpinner_ClickListener(new CommonSpinner.Spinner_ClickListener() {
            @Override
            public void ClickListener(String data) {
                if(data.equals(getResources().getString(R.string.DIY))){
                    spinner_factor3.getEditView().setText("");
                    EditText et=spinner_factor3.getEditView();
                    et.setFocusable(true);
                    et.setFocusableInTouchMode(true);
                    InputMethodManager inputManager =(InputMethodManager)et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if(inputManager!=null) {
                        et.requestFocus();
                        inputManager.showSoftInput(et, 0);
                    }
                }
            }
        });

        input_time = v.findViewById(R.id.input_time);
        input_business = v.findViewById(R.id.input_business);
        input_project = v.findViewById(R.id.input_project);
        input_remark = v.findViewById(R.id.input_remark);

        initTimerPicker();
        input_time.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                mTimerPicker.show(input_time.getText().toString());
            }
        });


        return v;
    }

    public Map<String,String> givedatas(){
        Map<String,String> mdataset = new HashMap<>();
        mdataset.put("amount",amount);
        mdataset.put("time",input_time.getText().toString());
        mdataset.put("lv1_option",spinner_factor.getText());
        mdataset.put("lv2_option",spinner_factor2.getText());
        mdataset.put("account",account_option);
        mdataset.put("member",spinner_factor3.getText());
        mdataset.put("business",input_business.getText().toString());
        mdataset.put("project",input_project.getText().toString());
        mdataset.put("remark",input_remark.getText().toString());
        return mdataset;
    }

    private void initTimerPicker() {
        String beginTime = "2000-1-1 00:00";
        String endTime = DateFormatUtils.long2Str(System.currentTimeMillis(), true);

        input_time.setText(endTime);

        // 通过日期字符串初始化日期，格式请用：yyyy-MM-dd HH:mm
        mTimerPicker = new CustomDatePicker(getActivity(), new CustomDatePicker.Callback() {
            @Override
            public void onTimeSelected(long timestamp) {
                input_time.setText(DateFormatUtils.long2Str(timestamp, true));
            }
        }, beginTime, endTime);
        // 允许点击屏幕或物理返回键关闭
        mTimerPicker.setCancelable(true);
        // 显示时和分
        mTimerPicker.setCanShowPreciseTime(true);
        // 允许循环滚动
        mTimerPicker.setScrollLoop(true);
        // 允许滚动动画
        mTimerPicker.setCanShowAnim(true);
    }

    public Fragment_outcome(Context context){
        this.context = context;
    }
}
